<?php 

require_once  __DIR__ . '/includes/authenticate.php';
include_once __DIR__ . '/db_bb_connect.php';

require_once __DIR__ . '/includes/BasketbalFunctions.php';
$basketbalfunctions = new BasketbalFunctions();

$type = $basketbalfunctions->GetUserLoginType();

if ( $type == 'Coach' ) {
	$user_key = $_POST['user_key'];
} else if ( $type == 'Player' ){
	$user_key = $_SESSION['user_key'];
}

$teamID = $basketbalfunctions->GetCurrentTeamIDFromPlayerKey($user_key);

$team_user_key = $basketbalfunctions->GetTeamUserKeyFromTeamID($teamID);

?>

<div class="container-fluid">
	
	<div class="row">
		<div class="col-md-4"></div>
		<div class="col-md-4">

			<h2 class="text-center">Basketball Theory</h2>
			<br>
<div id="articlecontent">
<?php 

$sql = "SELECT articleID 
FROM articles_visibility 
WHERE user_key = '$user_key'
OR user_key = '$team_user_key'";

$result = $db->query($sql); 

if ( mysqli_num_rows($result) == 0 ) {
	echo "No Articles Found";
} else {
 
while( $row = $result->fetch_assoc() ){

	$ID = $row['articleID'];

	$sql2 = "SELECT ID, title FROM articles WHERE ID = '$ID'";

	$result2 = $db->query($sql2);
	$row2 = $result2->fetch_assoc();

	$article_ID = $row2['ID']; 
	$article_title = $row2['title'];

 ?>
	<button class="btn btn-default btn-lga btn-block loadarticle" id="<?= $article_ID ?>"><?= $article_title ?></button> <br>	


<?php } ?>



<?php		
}
	

?>
</div>
			<br><br><br>
			</div>
		<div class="col-md-4"></div>
	</div>

</div>

<script type="text/javascript">
	$(".loadarticle").on("click", function(e){
		e.preventDefault();	
		var ID = $(this).attr("id");
		$.ajax({
	      type: "POST",
	        url: "./article_view_player.php",
	        data: { 'article_ID' : ID },
	        success: function(data) {    
	          $("#articlecontent").html(data);
	        },
	        error: function (xhr, status, error) {
	          $("#articlecontent").html(xhr.responseText);
	        }
	    });

	});
</script>